package com.ibeeking.found.content.b.rest.service.impl;

import com.ibeeking.found.content.api.common.dto.MemberInfoDTO;
import com.ibeeking.found.content.api.feign.ContentMemberBClient;
import com.ibeeking.found.content.b.rest.service.IBroadcastCommentService;
import com.ibeeking.found.content.b.rest.service.IBroadcastRoomService;
import com.ibeeking.found.content.service.common.dos.BroadcastCommentDO;
import com.ibeeking.found.content.service.common.query.BroadcastCommentPageQuery;
import com.ibeeking.found.content.service.common.query.BroadcastQuery;
import com.ibeeking.found.content.service.common.vo.BroadcastCommentVO;
import com.ibeeking.found.content.service.common.vo.BroadcastVO;
import com.ibeeking.found.content.service.mapper.BroadcastCommentMapper;
import com.ibeeking.nematos.utils.bean.BeanUtil;
import com.ibeeking.nematos.utils.data.Collections3;
import com.ibeeking.nematos.utils.json.JsonUtils;
import com.ibeeking.nematos.utils.result.ResponseResult;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.apache.commons.lang3.StringUtils;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.time.LocalDateTime;
import java.util.List;
import java.util.Map;
import java.util.Optional;
import java.util.Set;
import java.util.stream.Collectors;

/**
 * @ClassName BroadcastCommentServiceImpl
 * @Description
 * @Author ibeeking
 * @Date 2022-02-21 16:01
 **/
@Service
public class BroadcastCommentServiceImpl extends ServiceImpl<BroadcastCommentMapper, BroadcastCommentDO> implements IBroadcastCommentService {

    @Resource
    private BroadcastCommentMapper broadcastCommentMapper;
    @Resource
    private IBroadcastRoomService broadcastRoomService;
    @Resource
    private ContentMemberBClient contentMemberBClient;

    @Override
    public Page<BroadcastCommentVO> pageList(BroadcastCommentPageQuery broadcastCommentPageQuery) {
        LambdaQueryWrapper<BroadcastCommentDO> wrapper = new LambdaQueryWrapper<>();
        wrapper.eq(broadcastCommentPageQuery.getRoomId() != null, BroadcastCommentDO::getRoomId, broadcastCommentPageQuery.getRoomId());
        wrapper.ge(StringUtils.isNotBlank(broadcastCommentPageQuery.getCreateTimeStart()), BroadcastCommentDO::getCreateTime, broadcastCommentPageQuery.getCreateTimeStart());
        wrapper.le(StringUtils.isNotBlank(broadcastCommentPageQuery.getCreateTimeEnd()), BroadcastCommentDO::getCreateTime, broadcastCommentPageQuery.getCreateTimeEnd());
        wrapper.orderByDesc(BroadcastCommentDO::getCreateTime);
        Page<BroadcastCommentDO> pages = broadcastCommentMapper.selectPage(new Page<BroadcastCommentDO>(broadcastCommentPageQuery.getPageNum(), broadcastCommentPageQuery.getPageSize()), wrapper);
        Page<BroadcastCommentVO> broadcastCommentVOPage = BeanUtil.convertPage(pages, BroadcastCommentVO.class);
        List<BroadcastCommentVO> records = broadcastCommentVOPage.getRecords();
        wrapperVO(records);
        return broadcastCommentVOPage;
    }

    @Override
    public BroadcastCommentVO view(Long id) {
        BroadcastCommentDO broadcastCommentDO = broadcastCommentMapper.selectById(id);
        BroadcastCommentVO broadcastCommentVO = JsonUtils.fromJson(broadcastCommentDO, BroadcastCommentVO.class);
        wrapperVO(Collections3.newArrayList(broadcastCommentVO));
        return broadcastCommentVO;
    }

    @Override
    public Boolean changeStatus(Long id, Integer status) {
        BroadcastCommentDO broadcastCommentDO = new BroadcastCommentDO();
        broadcastCommentDO.setId(id);
        broadcastCommentDO.setPublishStatus(status);
        broadcastCommentDO.setModifyTime(LocalDateTime.now());
        return broadcastCommentMapper.updateById(broadcastCommentDO) > 0;
    }

    public void wrapperVO(List<BroadcastCommentVO> vos) {
        if (Collections3.isEmpty(vos)) {
            return;
        }
        Set<Integer> roomIds = vos.stream().map(BroadcastCommentVO::getRoomId).collect(Collectors.toSet());
        Set<Long> memberIds = vos.stream().map(BroadcastCommentVO::getMemberId).collect(Collectors.toSet());
        List<BroadcastVO> broadcastVOList = broadcastRoomService.list(new BroadcastQuery().setRoomIds(Collections3.newArrayList(roomIds)));
        Map<Integer, String> broadcastMap = broadcastVOList.stream().collect(Collectors.toMap(BroadcastVO::getRoomId, BroadcastVO::getRoomName));
        ResponseResult<List<MemberInfoDTO>> responseResult = contentMemberBClient.infoList(memberIds);
        List<MemberInfoDTO> memberInfoDTOList = Optional.ofNullable(responseResult.getData()).orElse(Collections3.emptyList());
        Map<Long, String> memberMap = memberInfoDTOList.stream().collect(Collectors.toMap(MemberInfoDTO::getId, x -> x.getNickname()));
        for (BroadcastCommentVO vo : vos) {
            vo.setRoomName(broadcastMap.get(vo.getRoomId()));
            vo.setMemberName(memberMap.get(vo.getMemberId()));
        }
    }
}
